import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/wroks/order_item_info.dart';
import 'package:app/src/http/bean/wroks/product_bean.dart';
import 'package:app/src/http/bean/wroks/special_clauses.dart';
import 'package:app/src/ui/route/helper/route_helper.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/a_combined_utils.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../../http/bean/wroks/carfreightrates_bean.dart';
import '../../../../app_ui.dart';

///外车运费上报明细
class UiCarFreightRatesItemInfo {
  ///List<OrderItemInfo>? 订单条目信息
  static Widget build(BuildContext context, List<FreightDetails>? data,
      {String? title}) {
    if (StringUtils.isEmpty(data)) {
      return AppUi.getEmptyView();
    }
    List<Widget> children = [];
    if (StringUtils.isEmpty(title)) {
      title = AppStrings.of(context).FreightreportDetails; //运费上报明细
    }
    children.add(
        UiBase.title(context, title, bottom: AppEdgeInsets.margin_bottom)); //标题
    children.add(_table(context, data));
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  static Widget _addItem(String? text,
      {bool isBlue = false, bool isTrim = true, GestureTapCallback? onTap}) {
    Widget child = Center(child: _text(text, isBlue: isBlue, isTrim: isTrim));
    if (onTap == null) {
      return child;
    }
    return InkWell(
      child: child,
      onTap: onTap,
    );
  }

  static Widget _text(String? text, {bool isBlue = false, bool isTrim = true}) {
    TextStyle? style = null;
    if (isBlue) {
      style = AppStyle.textStyleWithCardBlue();
    } else {
      style = AppStyle.textStyleWithCard();
    }
    String str;
    if (isTrim) {
      str = text?.trim() ?? "";
    } else {
      str = text ?? "";
    }
    return Text(
      str,
      style: style,
    );
  }

  ///小图标显示
  static Widget _icon(String? url, GestureTapCallback? onTap) {
    //print("图片：$url");
    //url="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic_source%2F9e%2F66%2Fd7%2F9e66d7c49718339d16eb75e368a90fd4.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1670735960&t=3d914ef246ce91c20cf9bf904afccde2";
    if (StringUtils.isEmpty(url)) {
      return AppUi.getEmptyView();
    }
    return InkWell(
      child: Image.network(
        url!,
        height: 40,
      ),
      onTap: () {
        onTap?.call();
      },
    );
  }

  ///fixme table嵌套table实现单元格合并；即：每一行都是table
  static Color _BorderColor = Colors.black12;

  ///背景色表格标题
  static Color _backgroundColorWithTableTitle = Colors.black12;

  ///背景色灰色,表格二级详情背景色
  static Color _backgroundColorWithTabelSecondary = ColorUtils.parseColor("#EAEAEA");
  static double _width = 1;
  static StateSetter? _setState = null;

  static Widget _table(BuildContext context, List<FreightDetails>? datas) {
    if (datas == null) {
      return AppUi.getEmptyView();
    }
    return StatefulBuilder(builder: (context, setState) {
      _setState = setState;
      var children = <TableRow>[];
      //fixme 第一行标题栏
      var tableRow = TableRow(children: [_title(context)]);
      children.add(tableRow);
      //fixme 第二行，内容行。
      for (int i = 0; i < datas.length; i++) {
        var data = datas[i];
        var tableRow = TableRow(children: [_content(data, i, context)]);
        children.add(tableRow);
        if (data.isOpenAn) {
          //fixme 嵌套表格
          //标题栏
          var tableRow = TableRow(children: [_secondaryTitle(context)]);
          children.add(tableRow);
          //内容行。
          var index = 0;
          data?.FreightList?.forEach((element) {
            var tableRow =
                TableRow(children: [_secondaryContents(element, index)]);
            index++;
            children.add(tableRow);
          });
        }
      }
      var table = Table(
        border: TableBorder(
          left: BorderSide(color: _BorderColor, width: _width), //fixme 左
          top: BorderSide(color: _BorderColor, width: _width), //fixme 上
          right: BorderSide(color: _BorderColor, width: _width), //fixme 右
          //bottom: BorderSide(color: _color, width: _width),////fixme 下
          //horizontalInside: BorderSide(color: _color, width: _width),//fixme 表格水平风格线
          //verticalInside: BorderSide(color: _color, width: _width),//fixme 表格垂直风格线
        ),
        defaultVerticalAlignment: TableCellVerticalAlignment.middle,
        // columnWidths: {
        //   0: IntrinsicColumnWidth(),
        //   2: IntrinsicColumnWidth(),
        // },
        children: children,
      );
      var container = Container(
        child: table,
        width: 1520,
      );
      //水平滑动效果
      var singleChildScrollView = SingleChildScrollView(
          child: container, scrollDirection: Axis.horizontal);
      return Center(
        child: singleChildScrollView,
      );
    });
  }

  //行标题
  static Widget _title(BuildContext context) {
    //fixme 标题栏
    var children = <TableRow>[];
    List<Widget> rowChildren = [];
    rowChildren.add(_addItem(AppStrings.of(context).Operation)); //操作
    rowChildren.add(_addItem(AppStrings.of(context).TheSerial)); //序号
    rowChildren.add(_addItem(AppStrings.of(context).CarCode)); //车牌号
    rowChildren.add(_addItem(AppStrings.of(context).TheName)); //姓名
    rowChildren.add(_addItem(AppStrings.of(context).Amount8)); //运费总额
    rowChildren.add(_addItem(AppStrings.of(context).PictureUrls)); //查看图片
    var tableRow = TableRow(
        decoration: BoxDecoration(color: _backgroundColorWithTableTitle),
        children: rowChildren);
    children.add(tableRow);
    var table = Table(
      border: TableBorder(
        // left: BorderSide(color: _color, width: _width),//fixme 左
        // top: BorderSide(color: _color, width: _width),//fixme 上
        // right: BorderSide(color: _color, width: _width),//fixme 右
        bottom: BorderSide(color: _BorderColor, width: _width),
        ////fixme 下
        //horizontalInside: BorderSide(color: _color, width: _width),//fixme 表格水平风格线
        verticalInside:
            BorderSide(color: _BorderColor, width: _width), //fixme 表格垂直风格线
      ),
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      children: children,
      columnWidths: {
        0: FixedColumnWidth(50), //FixedColumnWidth 固定一个宽度
        1: FixedColumnWidth(80),
      },
    );
    return table;
  }

  //行内容
  static Widget _content(
      FreightDetails element, int index, BuildContext context) {
    //fixme 内容行。
    List<Widget> rowChildren = [];
    if (!element.isOpenAn) {
      rowChildren.add(InkWell(
        child: Container(
          alignment: Alignment.center,
          height: 30,
          child: Icon(
            Icons.arrow_drop_down,
            color: Colors.blueAccent,
          ),
        ),
        onTap: () {
          //展开
          _setState?.call(() {
            element.isOpenAn = true;
          });
        },
      )); //操作
    } else {
      rowChildren.add(InkWell(
        child: Container(
          alignment: Alignment.center,
          height: 30,
          child: Icon(
            Icons.arrow_drop_up,
            color: Colors.blueAccent,
          ),
        ),
        onTap: () {
          //关闭
          _setState?.call(() {
            element.isOpenAn = false;
          });
        },
      )); //操作
    }
    rowChildren.add(_addItem((index + 1).toString())); //序号
    rowChildren.add(_addItem(element.CarCode)); //车牌号
    rowChildren.add(_addItem(element.Driver)); //姓名
    rowChildren.add(_addItem(element.Amount)); //运费总额
    rowChildren.add(_icon(element.getPictureUrl(), () {
      AppRouteHelper.goPreviewImagesRoute(context, element.PictureUrls); //图片预览
    })); //图形
    var tableRow = TableRow(children: rowChildren);
    var children = <TableRow>[];
    children.add(tableRow);
    var table = Table(
      // border: TableBorder.all(
      //     color: Colors.black, width: 1, style: BorderStyle.solid),
      border: TableBorder(
        //left: BorderSide(color: _color, width: _width),//fixme 左
        //top: BorderSide(color: _color, width: _width),//fixme 上
        //right: BorderSide(color: _color, width: _width),//fixme 右
        bottom: BorderSide(color: _BorderColor, width: _width),
        ////fixme 下
        //horizontalInside: BorderSide(color: _color, width: _width),//fixme 表格水平风格线
        verticalInside:
            BorderSide(color: _BorderColor, width: _width), //fixme 表格垂直风格线
      ),
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      children: children,
      columnWidths: {
        0: FixedColumnWidth(50), //FixedColumnWidth 固定一个宽度
        1: FixedColumnWidth(80),
      },
    );
    return table;
  }

  //嵌套：行标题
  static Widget _secondaryTitle(BuildContext context) {
    //fixme 标题栏
    List<Widget> rowChildren = [];
    rowChildren.add(_addItem(AppStrings.of(context).TheSerial)); //序号
    rowChildren.add(_addItem(AppStrings.of(context).CarCode2)); //装车号
    rowChildren.add(_addItem(AppStrings.of(context).InvoiceNo)); //发货单号
    rowChildren.add(_addItem(AppStrings.of(context).CustomerName)); //客户名称
    rowChildren.add(_addItem(AppStrings.of(context).ProjectName)); //项目名称
    rowChildren.add(_addItem(AppStrings.of(context).TWeight)); //总重量
    rowChildren.add(_addItem(AppStrings.of(context).TSquare)); //总平方
    rowChildren.add(_addItem(AppStrings.of(context).SmallHobNum)); //小铁架数
    rowChildren.add(_addItem(AppStrings.of(context).BigHobNum)); //大铁架数
    rowChildren.add(_addItem(AppStrings.of(context).Amount10)); //运费
    var children = <TableRow>[];
    var tableRow = TableRow(
        decoration: BoxDecoration(color: _backgroundColorWithTabelSecondary),
        children: rowChildren);
    children.add(tableRow);
    var table = Table(
      border: TableBorder(
        // left: BorderSide(color: _color, width: _width),//fixme 左
        //top: BorderSide(color: _BorderColor, width: _width),
        //fixme 上
        // right: BorderSide(color: _color, width: _width),//fixme 右
        bottom: BorderSide(color: _BorderColor, width: _width),
        ////fixme 下
        //horizontalInside: BorderSide(color: _color, width: _width),//fixme 表格水平风格线
        verticalInside:
            BorderSide(color: _BorderColor, width: _width), //fixme 表格垂直风格线
      ),
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      children: children,
      columnWidths: {
        0: FixedColumnWidth(80), //FixedColumnWidth 固定一个宽度
        1: FixedColumnWidth(160),
        2: FixedColumnWidth(160),
        3: FixedColumnWidth(260),
        4: FixedColumnWidth(260),
        5: FixedColumnWidth(100),
        6: FixedColumnWidth(100),
        7: FixedColumnWidth(100),
        8: FixedColumnWidth(100),
        9: FixedColumnWidth(100),
        10: FixedColumnWidth(100),
      },
    );
    return table;
  }

  //嵌套：行内容
  static Widget _secondaryContents(FreightListDetails element, int index) {
    //fixme 内容行。
    List<Widget> rowChildren = [];
    rowChildren.add(_addItem((index + 1).toString())); //序号
    rowChildren.add(_addItem(element.CarCode)); //装车号
    rowChildren.add(_addItem(element.InvoiceNo)); //发货单号
    rowChildren.add(_addItem(element.CustomerName)); //客户名称
    rowChildren.add(_addItem(element.ProjectName)); //项目名称
    rowChildren.add(_addItem(element.TWeight)); //总重量
    rowChildren.add(_addItem(element.TSquare)); //总平方
    rowChildren.add(_addItem(element.SmallHobNum)); //小铁架数
    rowChildren.add(_addItem(element.BigHobNum)); //大铁架数
    rowChildren.add(_addItem(element.Amount)); //运费
    var children = <TableRow>[];
    var tableRow = TableRow(children: rowChildren);
    children.add(tableRow);
    var table = Table(
      // border: TableBorder.all(
      //     color: Colors.black, width: 1, style: BorderStyle.solid),
      border: TableBorder(
        //left: BorderSide(color: _color, width: _width),//fixme 左
        //top: BorderSide(color: _color, width: _width),//fixme 上
        //right: BorderSide(color: _color, width: _width),//fixme 右
        bottom: BorderSide(color: _BorderColor, width: _width),
        ////fixme 下
        //horizontalInside: BorderSide(color: _color, width: _width),//fixme 表格水平风格线
        verticalInside:
            BorderSide(color: _BorderColor, width: _width), //fixme 表格垂直风格线
      ),
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      children: children,
      columnWidths: {
        0: FixedColumnWidth(80), //FixedColumnWidth 固定一个宽度
        1: FixedColumnWidth(160),
        2: FixedColumnWidth(160),
        3: FixedColumnWidth(260),
        4: FixedColumnWidth(260),
        5: FixedColumnWidth(100),
        6: FixedColumnWidth(100),
        7: FixedColumnWidth(100),
        8: FixedColumnWidth(100),
        9: FixedColumnWidth(100),
        10: FixedColumnWidth(100),
      },
    );
    return table;
  }
}
